package net.xdclass.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.component.MailService;
import net.xdclass.constant.CacheKey;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.util.CheckUtil;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import net.xdclass.service.NotifyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class NotifyServiceImpl implements NotifyService {

    @Autowired
    private MailService mailService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 十分钟
     */
    private static final int CODE_EXPIRED = 60 * 1000 * 10;
    /**
     * 验证码主题
     */
    private static final String SUBJECT = "验证码";
    /**
     * 验证码内容
     */
    private static final String CONTENT = "您的验证码是%s,有效时间10分钟,请勿告诉他人";

    /**
     * 前置：判断是否重复发送
     * 1.存储验证码到缓存
     * 2.发送邮箱验证码
     * 后置：存储发送记录
     * @param sendCodeType
     * @param to
     * @return
     */
    @Override
    public JsonData sendCode(SendCodeEnum sendCodeType, String to) {

        String cacheKey = String.format(CacheKey.CHECK_CODE_KEY, sendCodeType.name(),to);

        String cacheValue = redisTemplate.opsForValue().get(cacheKey);

        //如果部位空，则判断是否60秒内重复发送
        if (StringUtils.isNotBlank(cacheValue)){
            long ttl = Long.parseLong(cacheValue.split("_")[1]);
            //当前时间戳减去验证码发送的时间戳，小于600秒，则不重复发送
            if (CommonUtil.getCurrentTimestamp() - ttl < 60 * 1000 * 10){
                log.info("重复发送验证码，时间间隔:{}秒",(CommonUtil.getCurrentTimestamp() - ttl)/1000);
            }
        }

        //拼接验证码 2322_234535654
        String code = CommonUtil.getRandomCode(6);

        String value = code + "_" +CommonUtil.getCurrentTimestamp();

        redisTemplate.opsForValue().set(cacheKey, value, CODE_EXPIRED, TimeUnit.MILLISECONDS);
        if(CheckUtil.isEmail(to)){
            //邮箱验证码
            mailService.sendSimpleMail(to,SUBJECT,String.format(CONTENT,code));

            return JsonData.buildSuccess();

        }else if(CheckUtil.isPhone(to)){
            //短信验证码
        }

        return JsonData.buildResult(BizCodeEnum.CODE_TO_ERROR);
    }

    /**
     * 校验验证码
     * @param sendCodeEnum
     * @param to
     * @param code
     * @return
     */
    @Override
    public boolean checkCode(SendCodeEnum sendCodeEnum, String to, String code) {

        String cacheKey = String.format(CacheKey.CHECK_CODE_KEY,sendCodeEnum.name(),to);

        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        if (StringUtils.isNotBlank(cacheValue)){

            String cacheCode = cacheValue.split("_")[0];
            log.info("cacheCode:{}",cacheCode);
            if (cacheCode.equals(code)){
                //删除验证码
                redisTemplate.delete(cacheKey);
                return true;
            }
        }
        return false;
    }
}
